package com.qdxht.dispenser.ui.activity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivityTimerSwitchTestBinding;
import com.qdxht.dispenser.utils.BroadcastUtil;
import com.qdxht.dispenser.utils.ConfigUtil;
import com.hjq.toast.ToastUtils;
import android.widget.TimePicker;

public class TimerSwitchTestActivity extends BaseActivity<ActivityTimerSwitchTestBinding> {

    public static void start(android.content.Context context) {
        android.content.Intent intent = new android.content.Intent(context, TimerSwitchTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected ActivityTimerSwitchTestBinding getBinding() {
        return ActivityTimerSwitchTestBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        // 初始化数据
        boolean timerSwitch = ConfigUtil.getTimerSwitch();
        String enableTime = ConfigUtil.getEnableTime();
        String disableTime = ConfigUtil.getDisableTime();

        binding.radioEnable.setChecked(timerSwitch);
        binding.radioDisable.setChecked(!timerSwitch);
        binding.tvEnableTime.setText(enableTime.isEmpty() ? "请选择开机时间" : enableTime);
        binding.tvDisableTime.setText(disableTime.isEmpty() ? "请选择关机时间" : disableTime);

        binding.tvEnableTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker(binding.tvEnableTime);
            }
        });
        binding.tvDisableTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker(binding.tvDisableTime);
            }
        });

        binding.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEnable = binding.radioEnable.isChecked();
                String enableTime = binding.tvEnableTime.getText().toString().trim();
                String disableTime = binding.tvDisableTime.getText().toString().trim();
                ConfigUtil.setTimerSwitch(isEnable);
                ConfigUtil.setEnableTime(enableTime.equals("请选择开机时间") ? "" : enableTime);
                ConfigUtil.setDisableTime(disableTime.equals("请选择关机时间") ? "" : disableTime);
                ToastUtils.show("保存成功");
                BroadcastUtil.sendTimerSwitchBroadcast(mActivity);
                finish();
            }
        });
    }

    private void showTimePicker(final android.widget.TextView targetView) {
        String current = targetView.getText().toString();
        int hour = 0, minute = 0;
        if (current.matches("\\d{1,2}:\\d{2}")) {
            String[] parts = current.split(":");
            hour = Integer.parseInt(parts[0]);
            minute = Integer.parseInt(parts[1]);
        }
        TimePickerDialog dialog = new TimePickerDialog(this, (TimePicker view, int hourOfDay, int minute1) -> {
            String time = String.format("%02d:%02d", hourOfDay, minute1);
            targetView.setText(time);
        }, hour, minute, true);
        dialog.show();
    }
} 